package ltd.sumfsy.grain.controller;

import ltd.sumfsy.grain.bean.User;
import ltd.sumfsy.grain.decorate.ResultData;
import ltd.sumfsy.grain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 *
 * @author 范颂扬
 * @create 2021-06-10 19:49
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户注册
     *
     * @param username 用户名
     * @param password 密码
     * @param tel      电话号码
     * @return ltd.sumfsy.grain.decorate.ResultData
     * @author 范颂扬
     * @date 2021-07-06 16:05
     */
    @PutMapping("/register")
    public ResultData register(@RequestParam("username") String username
            , @RequestParam("password") String password
            , @RequestParam("tel") String tel, HttpSession session
            , @RequestParam(value = "isAdmin", required = false
            , defaultValue = "0") Integer isAdmin) {
        // 校验是否有权限创建管理员账号
        if (isAdmin == 1) {
            User userData = (User) session.getAttribute("userData");
            if (userData.getIsAdmin() == 0) {
                return ResultData.fail().setMessage("你没有权限创建管理员账号！！！");
            }
        }
        // 验证用户名和电话号码是否重复
        boolean isExistUsername = userService.isExistByUsername(username);
        boolean isExistTel = userService.isExistByTel(tel);
        if (isExistUsername) {
            return ResultData.fail().setMessage("用户名已经被注册");
        } else if (isExistTel) {
            return ResultData.fail().setMessage("电话号码已经被注册");
        }
        // 验证成功后，注册信息，写入数据库
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        boolean res = userService.register(new User().setUsername(username).setPassword(pwd)
                .setTelephone(tel).setIsAdmin(isAdmin));
        return res ? ResultData.success() : ResultData.fail();
    }

    /**
     * 获取用户绑定设备的实时数据
     *
     * @param id      绑定设备 ID
     * @param session 会话对象
     * @return ltd.sumfsy.grain.decorate.ResultData
     * @author 范颂扬
     * @date 2021-07-06 16:23
     */
    @GetMapping("/realTimeData/{id}")
    public ResultData getRealTimeData(@PathVariable("id") Integer id, HttpSession session) {
        return ResultData.success();
    }

    /**
     * 获取用户绑定设备的历史数据
     *
     * @param id        绑定设备 ID
     * @param session   会话对象
     * @param beginTime 开始时间（非必要字段）
     * @param endTime   结束时间（非必要字段）
     * @return ltd.sumfsy.grain.decorate.ResultData
     * @author 范颂扬
     * @date 2021-07-06 16:25
     */
    @GetMapping("/historicalData/{id}")
    public ResultData getHistoricalData(@PathVariable("id") Integer id, HttpSession session
            , @RequestParam(value = "beginTime", required = false) String beginTime
            , @RequestParam(value = "endTime", required = false) String endTime) {
        return ResultData.success();
    }

    /**
     * 绑定设备
     *
     * @param id
     * @param session
     * @return ltd.sumfsy.grain.decorate.ResultData
     * @author 范颂扬
     * @date 2021-07-06 16:27
     */
    @PutMapping("/binding")
    public ResultData bindingEquipment(@RequestParam("id") Integer id
            , HttpSession session) {
        return ResultData.success();
    }

    /**
     * 解除绑定
     *
     * @param id
     * @return ltd.sumfsy.grain.decorate.ResultData
     * @author 范颂扬
     * @date 2021-07-06 16:31
     */
    @DeleteMapping("/removeBinding/{id}")
    public ResultData removeBinding(@PathVariable("id") Integer id) {
        return ResultData.success();
    }

    /**
     * 用户反馈信息
     *
     * @param msg     反馈的信息
     * @param session 会话对象
     * @return ltd.sumfsy.grain.decorate.ResultData
     * @author 范颂扬
     * @date 2021-07-06 16:45
     */
    @PutMapping("/feedback")
    public ResultData feedback(@RequestParam("msg") String msg, HttpSession session) {
        return ResultData.success();
    }

}
